<?php

f()->import('form');

class _F_EXTENSION_UPLOAD_ extends _F_EXTENSION_ {
	
	public $prev_route;
	public $temp_path = '/runtime/upload';
	public $upload_path = '/files';
	public $default_preview = '/extensions/upload/images/preview.png';
	
	public function __construct() {
		f()->template_path_list[] = dirname(__FILE__).'/templates';
		f()->register_method('upload',array($this,'upload'));
		f()->form()->register_widget('file',array($this,'file'))
			->register_widget('image',array($this,'image'));
		$this->prev_route = f()->register_method(
			'route',array($this,'route'));		
	}

	public function route() {
		$r = f()->request;
		if ($r === '/upload') {
			f()->_f_route_($r,'/extensions/upload/actions');
		} else {
			call_user_func($this->prev_route);
		}
	}
	
	public function upload() {
		return $this;
	}

	public function file_extension($file_name) {
		$dot_pos = strrpos($file_name,'.');
		if ($dot_pos === false) {
			$ext = '';
			$fn = $file['name'];
		} else {
			$ext = substr($file_name,$dot_pos);
			$fn = substr($file_name,0,$dot_pos);
		}
	}
		
	public function file($id,$label,$params = array()) {
		f()->style('/extensions/upload/styles/upload.less');
		f()->import('jquery');
		f()->jquery();
		f()->js('$_fExtensionUploadUrl_',f()->url('@upload'));
		f()->script('/extensions/upload/scripts/upload.js');
		$parent = $this->_f_this_;
		$form = $parent->form;
		$parent->elements[$id] = array_merge($form->default_properties,array(
			'_f_value_saver_' => array('_F_EXTENSION_UPLOAD_','_f_value_saver_'),
			'template' => 'upload.file',
		),$params,array('id' => $id, 'widget' => 'file','label' => $label));
		return $parent;
	}

	public function image($id,$label,$params = array()) {
		f()->style('/extensions/upload/styles/upload.less');
		f()->import('jquery');
		f()->jquery();
		f()->js('$_fExtensionUploadUrl_',f()->url('@upload'));
		f()->script('/extensions/upload/scripts/upload.js');
		$parent = $this->_f_this_;
		$form = $parent->form;
		$parent->elements[$id] = array_merge($form->default_properties,array(
			'_f_value_saver_' => array('_F_EXTENSION_UPLOAD_','_f_value_saver_'),
			'template' => 'upload.image',
			'types' => 'jpeg,jpg,png,gif',
			'default_preview' => f()->upload()->default_preview,
		),$params,array('id' => $id, 'widget' => 'image','label' => $label));
		return $parent;
	}

	public static function save($id) {
		if (isset($id)) {
			if(!isset($_SESSION)) session_start();
			if (isset($_SESSION['_f_upload_file_'.$id])) {
				// save temp file
				$info = $_SESSION['_f_upload_file_'.$id];
				$upload_path = f()->upload()->upload_path;				
				$dir = f()->base_path.$upload_path;
				$path = substr(md5($id),0,2).'/'.$id;
				$fdir = $dir.'/'.$path;
				if (!file_exists($fdir)) mkdir($fdir,0777,true);
				// TODO: check if the directory created succesfully 
				// otherwise return error message
				$id = $path.'/'.$info['name'];
				rename($info['path'],$dir.'/'.$id);
			}			
		}
		return $id;
	}
	
	public static function _f_value_saver_($form,$id,$element,&$model,$fields) {
		if (isset($element['value'])) {
			$value = _F_EXTENSION_UPLOAD_::save($element['value']);
			$name = isset($element['field']) ? $element['field'] : $id;
			if (is_array($model)) {
			 	$model[$name] = $value;
			} else {
				$model->$name = $value;
			}
		}
	}
	
}

// TODO: a feleslegesen ottmaradt fileok torlese a session lezarasakor 

new _F_EXTENSION_UPLOAD_;
